//
//  ZDDRefresh.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/7/1.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
let ZDDRefreshHight: CGFloat = 50
// 刷新控件的状态枚举
enum ZDDRefreshType: Int{
    // 正常
    case Normal = 0
    // 下拉中
    case Pulling = 1
    // 刷新中
    case Refreshing = 2
}
class ZDDRefresh: UIControl {

    // 被观察者
    var scrollView: UIScrollView?
    var refreshState : ZDDRefreshType = .Normal{
        didSet{
        
            switch refreshState{
            
            case .Normal:
                messageLabel.text = "正常"
                 // 如果我们上一个状态为刷新中才开始动画
                if oldValue == .Refreshing {
                
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.scrollView?.contentInset.top -= ZDDRefreshHight
                       
                    })
                
                
                }
                
            case .Pulling :
                messageLabel.text = "下拉中..."
            case .Refreshing:
                messageLabel.text = "刷新中..."
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.scrollView?.contentInset.top += ZDDRefreshHight
                    
                    }, completion: { (_) -> Void in
                        //设置监听
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                })
            
            
            }
        
        
        }
    
        
    
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: -ZDDRefreshHight, width: kScreenW, height: ZDDRefreshHight))
         setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        backgroundColor = UIColor.darkGrayColor()
    
        addSubview(messageLabel)
    }
    // MARK: - 供外界调用方法
    func endRefreshing(){
        refreshState = .Normal
    }
    override func willMoveToSuperview(newSuperview: UIView?) {
        guard let scrollView = newSuperview as? UIScrollView else{
        return
        }
        self.scrollView = scrollView
        
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let contentOffSetY = self.scrollView!.contentOffset.y
        
        
        let contentOffSetTop = self.scrollView!.contentInset.top
        
        if scrollView!.dragging {
        
            if contentOffSetY > -contentOffSetTop - ZDDRefreshHight && refreshState == .Pulling{
            
            refreshState = .Normal
            
            }else if contentOffSetY < -contentOffSetTop - ZDDRefreshHight && refreshState == .Normal{
            
                refreshState = .Pulling
                
            }else{
            
            
            
                if refreshState == .Pulling{
                
                refreshState = .Refreshing
                
                }
            
            
            }
        
        
        
        
        }
        
        
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        messageLabel.frame = CGRect(x: 0, y: 0, width: kScreenW, height: ZDDRefreshHight)
    }
    
    private lazy var messageLabel: UILabel = {
    
        let lab = UILabel()
        lab.text = "正常"
        lab.textColor = UIColor.whiteColor()
        lab.textAlignment = .Center
        lab.font = UIFont.systemFontOfSize(15)
        return lab
    
    
    
    }()
    
    deinit{
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
}



















